package com.example.servlet;


import com.example.pojo.Ent;
import com.example.pojo.User;
import com.example.service.EntService;
import com.example.service.Impl.EntServiceImpl;
import com.example.service.Impl.UserServiceImpl;
import com.example.service.UserService;
import com.example.util.BackContent;
import com.example.util.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/userLogin")
public class UserServlet extends BaseServlet {
    /**
     * 将service层的相关方法引入在外部
     */
    UserService userService=new UserServiceImpl();
    EntService entService=new EntServiceImpl();


    /**
     * 用户登录的servlet方法
     * @param request
     * @param response
     * @return
     */
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("userName");
        String password = request.getParameter("password");
        User user = userService.UserLogin(username, password);
        String dz="";
        if (user!=null){
            dz="homePage";
            List<Ent> ents = entService.EntAll();
            request.setAttribute("entAll",ents);
        }else{
            dz="error";
        }
        return dz;
    }

    /**
     * 用户新增的信息
     * @param request
     * @param response
     * @return
     */

    public BackContent UserAdd(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("userName");
        String password = request.getParameter("password");
        String s = userService.UserAdd(name, password);
        BackContent backContent = new BackContent();
        if (s.equals("注册成功")){
            backContent.setZt(200);
            backContent.setZtContent("注册成功");
        }else {
            backContent.setZt(500);
            backContent.setZtContent("注册失败");
        }
        return backContent;
    }

    @Override
    public Class<?> getServletClass() {
        return  UserServlet.class;
    }
}
